import tensorflow as tf
import pathlib
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.model_selection import train_test_split
imgdir_path=pathlib.Path('C:/Users/csian/tensorflow_datasets/downloads/manual/img_align_celeba')
file_list=sorted([str(path) for path in imgdir_path.glob('*.jpg')])
fp=open('C:/Users/csian/tensorflow_datasets/downloads/manual/list_attr_celeba.txt', 'r')
line=fp.readline()
leng=int(line)
line=fp.readline()
index=line.split(' ')
loc=index.index('Male')
sex=[]
for i in range(leng):
line=fp.readline()
line=line[11:-1]
line=line.replace(' ', ' ')
line=line.split(' ')
if line[loc]=='-1':
li=0
else:
li=1
sex.append(li)
fp.close()
labels=tf.convert_to_tensor(sex)
tensor_list=[]
for i in range(30):
image=Image.open(file_list[i])
data=np.array(image)
td=tf.convert_to_tensor(data)
tensor_list.append(td)
print('%d / %d' %(i+1, len(sex)))
ds_test=tf.data.Dataset.from_tensor_slices(tensor_list[:15])
ds_test_y=tf.data.Dataset.from_tensor_slices(labels[:15])
ds_train=tf.data.Dataset.from_tensor_slices(tensor_list[15:])
ds_train_y=tf.data.Dataset.from_tensor_slices(labels[15:])
ds_train_joint=tf.data.Dataset.zip((ds_train, ds_train_y))
ds_test_joint=tf.data.Dataset.zip((ds_test, ds_test_y))
ds_train_batch=ds_train_joint.batch(15)
fig=plt.figure(figsize=(10, 8))
images, labels=next(iter(ds_train_batch))
for i, (image, label) in enumerate(zip(images, labels)):
ax=fig.add_subplot(3, 5, i+1)
ax.set_xticks([]);ax.set_yticks([])
ax.imshow(image)
ax.set_title('{}'.format(label), size=15)
plt.show()